package mao.leetcode.others.leetcode91;

public class Solution {

    public int numDecodings(String s) {
        if (s.trim().equals("")) return 0;
        char[] chars = s.toCharArray();
        int pre=1,cur=1,temp; //f(-1)=1,f(0)=1
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '0'){   //当前为0
                if (i>0 && (chars[i-1]=='1' || chars[i-1]=='2')){
                    cur=pre;
                    continue;
                } else
                    return 0;
            }
            if ( i>0 && ((chars[i-1]=='2' && chars[i]<='6' )  || chars[i-1]=='1')) //当前字符不为零
                 temp=cur+pre;
            else
                temp=cur;
            pre=cur;
            cur=temp;
        }
        return cur;
    }
}
